import UIKit

class MainViewController: UIViewController {
    
    private let tableView: UITableView = {
        let table = UITableView(frame: .zero, style: .insetGrouped)
        table.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        return table
    }()
    
    // 定义不同的 Markdown 样式示例
    private let examples: [(title: String, description: String, content: String)] = [
        ("基础语法", "展示基本的 Markdown 语法效果", TestResource.basicMarkdown),
        ("代码块", "展示代码块的不同样式", TestResource.codeBlockMarkdown),
        ("自定义标签", "展示自定义标签的使用", TestResource.customTagMarkdown),
        ("图片展示", "展示图片的使用和效果", TestResource.imageMarkdown),
        ("链接展示", "展示各种链接的使用", TestResource.linkMarkdown),
        ("列表样式", "展示有序和无序列表", TestResource.listMarkdown),
        ("引用展示", "展示引用的使用和效果", TestResource.quoteMarkdown),
        ("表格展示", "展示表格的使用和效果", TestResource.tableMarkdown),
        ("文本交互", "展示富文本的交互效果", TestResource.interactionMarkdown)
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    private func setupUI() {
        title = "Markdown 示例"
        view.backgroundColor = .systemBackground
        
        if #available(iOS 13.0, *) {
            navigationController?.navigationBar.prefersLargeTitles = true
        }
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
//            make.left.right.equalToSuperview()
//            make.bottom.equalToSuperview().offset(-100)
//            make.top.equalToSuperview()
        }
        
        tableView.delegate = self
        tableView.dataSource = self
        
//        let button = UIButton(type: .system)
//        button.setTitle("点击我", for: .normal)
//        button.setTitleColor(UIColor.red, for: .normal)
//        button.addTarget(self, action: #selector(testTapFunc(btn:)), for: .touchUpInside)
//        view.addSubview(button)
//        
//        button.snp.makeConstraints { make in
//            make.width.equalTo(140)
//            make.height.equalTo(60)
//            make.centerX.equalToSuperview()
//            make.bottom.equalToSuperview().offset(-20)
//        }
    }
    
    @objc
    func testTapFunc(btn:UIButton) {
        btn.backgroundColor = .green
        btn.setTitleColor(UIColor.white, for: .normal)
        print("test --- test")
    }
}


extension MainViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return examples.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        let example = examples[indexPath.row]
        
        if #available(iOS 14.0, *) {
            var config = cell.defaultContentConfiguration()
            config.text = example.title
            config.secondaryText = example.description
            cell.contentConfiguration = config
        } else {
            cell.textLabel?.text = example.title
            cell.detailTextLabel?.text = example.description
        }
        
        cell.accessoryType = .disclosureIndicator
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let example = examples[indexPath.row]
        let detailVC = MarkdownDetailViewController(
            title: example.title,
            markdownContent: example.content
        )
        navigationController?.pushViewController(detailVC, animated: true)
    }
} 
